package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Task;
import org.example.reemploymentsys.entity.TrainingDocument;
import org.example.reemploymentsys.entity.TrainingPlan;
import org.example.reemploymentsys.repository.TaskRe;
import org.example.reemploymentsys.repository.TrainingDocumentRe;
import org.example.reemploymentsys.repository.TrainingPlanRe;
import org.example.reemploymentsys.repository.UserTbRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TaskController {
    @Autowired
    UserTbRe userTbRe;
    @Autowired
    private TaskRe taskRe;
    @Autowired
    private TrainingPlanRe trainingPlanRe;
    @Autowired
    private TrainingDocumentRe trainingDocumentRe;
    @PostMapping("/getTasks/{id}")
    public List<Task> getTasks(@PathVariable Long id){
        System.out.println(id);
        return taskRe.findByTrainingPlanId(id);
    }
    @PostMapping("/addTask/{id}")
    public List<Task> addTask(@PathVariable Long id, @RequestBody Task task){
        TrainingPlan trainingPlan=trainingPlanRe.findById(id).orElse(null);
        task.setTrainingPlan(trainingPlan);
        taskRe.save(task);
        System.out.println(taskRe.findByTrainingPlanId(id));
        return taskRe.findByTrainingPlanId(id);
    }
    @PostMapping("/deleteTask/{id}")
    public List<Task> deleteTask(@PathVariable Long id){
        Long tId= taskRe.findTrainPlanIdByTaskId(id);
        System.out.println(tId);
        taskRe.deleteById(id);
        return taskRe.findByTrainingPlanId(tId);
    }
    @PostMapping("/getResourceList")
    public List<TrainingDocument> getResourceList(@RequestBody String username){
        System.out.println(username);
        String name= username.replace("\"","").replace("\"","");
        System.out.println(name);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(name);
        System.out.println(OrganizationId);
        return trainingDocumentRe.findByOrganizationIdOrRole(Long.parseLong(OrganizationId),"superAdmin");
    }

}
